package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.service.UserinfoService;
import com.xmg.p2p.base.util.RequireLogin;
import com.xmg.p2p.base.util.UserContext;
import com.xmg.p2p.business.domain.UserBankinfo;
import com.xmg.p2p.business.service.IUserBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.p2p.base.controller
 * @author: Andy
 * @date: 2021.4.17 14:02
 * @Description: 绑定银行卡控制器
 * @since JDK 1.8
 */
@Controller
public class UserBankController {
    @Autowired
    private UserinfoService userinfoService ;
    @Autowired
    private IUserBankService userBankService;

    /**
     * 导向到绑定银行卡页面
     * @param model 模型
     * @return  银行卡页面
     */
    @RequireLogin
    @RequestMapping("bankInfo")
    public String bankInfo(Model model){
        // 获得当前用户信息
        Userinfo current = this.userinfoService.getById(UserContext.getCurrent().getId());
        if ( current.getIsBindBank()) { //表示用户已经绑定银行卡了
            model.addAttribute("bankInfo",this.userBankService.getBankinfoByUser(current.getId()));
            return "bankInfo_result";
        }else{
            model.addAttribute("userinfo",current);
            return "bankInfo";
        }
    }

    /**
     * 绑定银行卡
     * @param userBankinfo  银行卡的封装对象
     * @return  返回到绑定成功页面。
     */
    @RequestMapping("bankInfo_save")
    public String bindBankinfo(UserBankinfo userBankinfo){
        this.userBankService.bindBank(userBankinfo);
        return "redirect:/bankInfo.do" ;
    }
}